/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import utils.HibernateUtil;
import pojo.Phim;

/**
 *
 * @author Hnmtuan
 */
public class PhimDAO {

    public static SessionFactory _localSessionFactory;
    public static Session _localSession;

    static {
        _localSessionFactory = (_localSessionFactory != null) ? _localSessionFactory : HibernateUtil.getSessionFactory();
    }

    public static void openCurrentSession() throws Exception {
        try {
            if (_localSession != null) {
                _localSession.close();
            }
            _localSession = _localSessionFactory.openSession();
        } catch (Exception e) {
            throw new Exception(String.format("Failes in %s of %s : %s", "openCurrentSession", PhimDAO.class.getName(), e.getMessage()));
        }
    }

    public static void closeCurrentSession() throws Exception {
        if (_localSession == null) {
            return;
        }
        try {
            _localSession.close();            
        } catch (Exception e) {
            throw new Exception(String.format("Failes in %s of %s : %s", "closeCurrentSession", PhimDAO.class.getName(), e.getMessage()));
        }
        finally{
            _localSession = null;
        }
    }

    public static List<Phim> getPhims() throws Exception {
        List<Phim> items = null;
//        Session localSession = null;
        String localQuery = "FROM Phim p WHERE p.trangThai =:statusPhim";
        try {
            // Step 01 : create retrieving session using where clause.
//            localSession = _localSessionFactory.openSession();
            if (_localSession == null) {
                openCurrentSession();
            }
            // Step 02 : begin retrieving transaction in above session.
            _localSession.beginTransaction();
            // Step 03 : set parameters and retrieving objects.
            Query q = _localSession.createQuery(localQuery);
            q.setInteger("statusPhim", 1);
            items = q.list();
            // Step 04 : commit and end retrieving transaction in above session.
            _localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s ", "getPhims", PhimDAO.class.getName(), e.getMessage()));
        } finally {
//            try {
//                //Step 05 : close retrieving session
//                localSession.close();
//            } catch (Exception e) {
//                throw new Exception(String.format("Failes in %s of %s : %s", "getPhims", PhimDAO.class.getName(), e.getMessage()));
//            }
        }
        return items;
    }

    public static List<Phim> getPhimsByCodePhim(String codePhim) throws Exception {
        List<Phim> items = null;
        Session localSession = null;
        String localQuery = "FROM Phim p WHERE p.codePhim = :codePhim AND p.trangThai = :statusPhim";
        try {
            //Step 01: validate input params.
            if (codePhim.isEmpty()) {
                throw new Exception("Param not valid");
            }
            //Step 02: creat retrieving session using where clause.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin retrieving transaction in above session.
            localSession.beginTransaction();
            //Step 04: set parameters and retrieving objects.
            Query q = localSession.createQuery(localQuery);
            q.setString("codePhim", codePhim);
            q.setInteger("statusPhim", 1);
            items = q.list();
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            throw new Exception(String.format("Failed in %s of %s : %s", "getPhimsByCodePhim", PhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close retrieving session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "getPhimsByCodePhim", PhimDAO.class.getName(), e.getMessage()));
            }
        }
        return items;
    }

    public static boolean insertPhims(Phim paramPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramPhims == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create insert session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin insert transaction in above session.
            localSession.beginTransaction();
            //Step 04: save object into database.
            localSession.save(paramPhims);
            //Step 05: commit and end insert transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "insertPhims", PhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close insert session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "insertPhims", PhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean updatePhims(Phim paramPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramPhims == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create update session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin update transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.update(paramPhims);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "updatePhims", PhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close update session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "updatePhims", PhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }

    public static boolean deletePhims(Phim paramPhims) throws Exception {
        boolean rs = true;
        Session localSession = null;
        try {
            //Step 01: validate input params.
            if (paramPhims == null) {
                throw new Exception("Param not valid");
            }
            //Step 02: create delete session.
            localSession = _localSessionFactory.openSession();
            //Step 03: begin delete transaction in above session.
            localSession.beginTransaction();
            //Step 04: update object into database.
            localSession.delete(paramPhims);
            //Step 05: commit and end update transaction in above session.
            localSession.getTransaction().commit();
        } catch (Exception e) {
            rs = false;
            throw new Exception(String.format("Failed in %s of %s : %s", "deletePhims", PhimDAO.class.getName(), e.getMessage()));
        } finally {
            try {
                //Step 06: close update session.
                localSession.close();
            } catch (Exception e) {
                throw new Exception(String.format("Failed in %s of %s : %s", "deletePhims", PhimDAO.class.getName(), e.getMessage()));
            }
        }
        return rs;
    }
}
